//
//  MKHomeViewController.swift
//  MKWeiBo2017
//
//  Created by nicolas on 2017/5/8.
//  Copyright © 2017年 nicolas. All rights reserved.
//

import UIKit

class MKHomeViewController: MKBaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if !MKUserAccountViewModel.sharedInstance.isUserLogin {
            return
        }
        setupNavigationBar()
    }
    
    func setupNavigationBar() {
        //
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: #selector(MKHomeViewController.didClickFriendsearch))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(MKHomeViewController.didClickPop))
        
        let name = MKUserAccountViewModel.sharedInstance.userAccount?.screen_name ?? "未知名称"
        
        // 设置首页titleView
        let button = MKHomeTitleButton()
        button.setTitle(name, for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.setImage(UIImage(named: "navigationbar_arrow_down"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(didClickTitlView(button:)), for: UIControlEvents.touchUpInside)
        
        button.sizeToFit()
        
        navigationItem.titleView = button
    }
    
    func didClickFriendsearch() {
        MKPrint(items: "点击了左边")
    }
    
    func didClickPop() {
        MKPrint(items: "点击了右边")
    }
    
    func didClickTitlView(button:UIButton) {
        
        button.isSelected = !button.isSelected
        
        UIView.animate(withDuration: 0.25) { () -> Void in
            let transfrom = button.isSelected ? CGAffineTransform(rotationAngle: CGFloat(M_PI - 0.001)) : CGAffineTransform.identity
            
            button.imageView?.transform = transfrom
        }
    
    }
    
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }
}
